package com.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.common.core.domain.AjaxResult;
import com.campus.mapper.CategoryMapper;
import com.campus.pojos.domain.ClientCategory;
import com.campus.pojos.domain.dto.CategoryQueryDto;
import com.campus.result.PageResult;
import com.campus.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Slf4j
@Transactional
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, ClientCategory> implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    /**
     * 管理端编辑墙
     * @param category
     * @return
     */
    public void edit(ClientCategory category) {
        categoryMapper.update(category);
    }

    /**
     * 墙分类查询
     * @param dto
     * @return
     */
    @Override
    public AjaxResult getList(CategoryQueryDto dto) {
        if(dto.getPage() == null) dto.setPage(1);
        if(dto.getPageSize() == null) dto.setPageSize(10);

        Page<ClientCategory> page = new Page<>(dto.getPage(), dto.getPageSize());
        QueryWrapper<ClientCategory> qw = new QueryWrapper<>();
        if(dto.getCategoryName() != null) {
            qw.like("category_name", dto.getCategoryName());
        }

        page = categoryMapper.selectPage(page, qw);

        return AjaxResult.success("success", new PageResult(page.getTotal(), page.getRecords()));
    }
}
